﻿using System;
using System.Collections.Generic;
using System.Text;
using EmbeddedSharp.Core;

namespace EmbeddedSharp.Scripting.Function.Context {

    /// <summary>
    /// Clear the variables in context
    /// </summary>
    [Function("clear", "Clears the variables in context", "clear(\"var\")", "")]
    public class ClearNode : ContextFuncNode {

        public ClearNode()
            : base("clear") {
        }

        public override object Evaluate() {
            if (Parameters.Count == 0) {
                base.Context.Clear();
            } else {
                foreach (Node node in Parameters) {
                    if (base.Context.HasVariable(node.Value)) {
                        base.Context.RemoveVariable(node.Value);
                    } else if (base.Context.GetFunctionManager().Exist(node.Value)) {
                        base.Context.GetFunctionManager().Remove(node.Value);
                    }
                }
            }

            return string.Empty;
        }
    }
}
